

import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.SWT;

import db.DBManager;


/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */



public class SelectDialog extends org.eclipse.swt.widgets.Dialog {

	private Shell dialogShell;
	private Group group;
	private List unChosenList;
	private List selectedList;
	private Label selectLabel;
	private Button cancelButton;
	private Button OKbutton;
	private Button removeButton;
	private Button addButton;
	DBManager dbm = new DBManager();
	private Listener listener;
	private static Shell parent;
	private String[] retVal;


	/**
	 * Auto-generated main method to display this 
	 * org.eclipse.swt.widgets.Dialog inside a new Shell.
	 */
	public static void main(String[] args) {
		try {

			String[] str = new String[1];
			Display display = Display.getDefault();
			Shell shell = new Shell(display);
			SelectDialog inst = new SelectDialog(shell, SWT.NULL);
			inst.open(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public SelectDialog(Shell parent, int style) {
		super(parent, style);
	}

	public String[] open(String[] str) {
		try {
			retVal = str;
			parent = getParent();
			dialogShell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
			dialogShell.setLayout(new FormLayout());
			dialogShell.layout();
			dialogShell.pack();			
			dialogShell.setSize(531, 363);
			{
				group = new Group(dialogShell, SWT.NONE);
				group.setLayout(null);
				FormData groupLData = new FormData();
				groupLData.left =  new FormAttachment(0, 1000, 12);
				groupLData.top =  new FormAttachment(0, 1000, 12);
				groupLData.width = 485;
				groupLData.height = 284;
				group.setLayoutData(groupLData);
				group.setText("Choose tags:");
				{
					unChosenList = new List(group, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
					unChosenList.setBounds(8, 40, 204, 216);
					String[] list = dbm.getAllTags();
					unChosenList.setItems(list);
				}
				{
					selectedList = new List(group, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
					selectedList.setBounds(278, 40, 201, 216);
				}
				{
					addButton = new Button(group, SWT.PUSH | SWT.CENTER);
					addButton.setText(">");
					addButton.setBounds(218, 86, 48, 30);
					listener = new Listener()
					{

						@Override
						public void handleEvent(Event event) 
						{
							if(event.widget == addButton)
							{
								String[] addList = unChosenList.getSelection();
								int[] indexes = unChosenList.getSelectionIndices();
								for(int i = 0;i<addList.length;i++)
								{
									selectedList.add(addList[i]);
								}
								unChosenList.remove(indexes);
							}
						}
					};
					//	update();
					addButton.addListener(SWT.Selection, listener);

				}
				{
					removeButton = new Button(group, SWT.PUSH | SWT.CENTER);
					removeButton.setText("<");
					removeButton.setBounds(218, 128, 48, 30);
					listener = new Listener()
					{

						@Override
						public void handleEvent(Event event) 
						{
							if(event.widget == removeButton)
							{
								String[] removeList = selectedList.getSelection();
								int[] indexes = selectedList.getSelectionIndices();
								for(int i = 0;i<removeList.length;i++)
								{
									unChosenList.add(removeList[i]);
								}
								selectedList.remove(indexes);
							}
						}
					};
					//	update();
					removeButton.addListener(SWT.Selection, listener);
				}
				{
					OKbutton = new Button(group, SWT.PUSH | SWT.CENTER);
					OKbutton.setText("OK");
					OKbutton.setBounds(347, 269, 60, 20);
					//	return selectedList.getItems();
					listener = new Listener()
					{

						@Override
						public void handleEvent(Event event) 
						{
							if(event.widget == OKbutton)
							{
								retVal = selectedList.getItems();
								dialogShell.getShell().close();

							}
						}
					};
					//	update();
					OKbutton.addListener(SWT.Selection, listener);


				}
				{
					cancelButton = new Button(group, SWT.PUSH | SWT.CENTER);
					cancelButton.setText("Cancel");
					cancelButton.setBounds(419, 269, 60, 20);
					listener = new Listener()
					{

						@Override
						public void handleEvent(Event event) 
						{
							if(event.widget == cancelButton)
							{
								System.out.println("CANCEL");
							//	retVal = null;
								//		dialogShell.getParent().dispose();
								dialogShell.getShell().close();
								
							}
						}
					};
					//	update();
					cancelButton.addListener(SWT.Selection, listener);

				}



				{
					selectLabel = new Label(group, SWT.NONE);
					selectLabel.setText("Selected:");
					selectLabel.setBounds(278, 18, 60, 22);
				}
				
			}

			dialogShell.setLocation(getParent().toDisplay(100, 100));
			dialogShell.open();
			Display display = dialogShell.getDisplay();
			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		
		return retVal;
		//return null;
		
	}

}
